home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / WindowsService / ProjectInstaller.pas next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  4.1 KB  |  141 lines

  1. unit ProjectInstaller;
  2. //------------------------------------------------------------------------------
  3. //  File name:      ProjectInstaller.pas
  4. //  Last updated:   11/9/03
  5. //  Author:         Sergey Mishkovskiy
  6. //  Company:        USysWare, Inc.
  7. //  Contact info:   usysware@comcast.net
  8. //
  9. //  Compatibility:  Borland Delphi for .NET
  10. //
  11. //  Description:    Consists of a TServiceInstaller class that encapsulates 
  12. //                  .NET service installer functionality. When installing the 
  13. //                  service, the install utility uses reflection to discover 
  14. //                  all Installer class descendants that have RunInstaller 
  15. //                  attribute set. It's important that installer's ServiceName 
  16. //                  is kept in sync with service's ServiceName.
  17. //------------------------------------------------------------------------------
  18.  
  19. interface
  20.  
  21. uses
  22.   System.ServiceProcess,
  23.   System.Configuration.Install,
  24.   System.ComponentModel,
  25.   System.Collections,
  26.   Microsoft.Win32;
  27.  
  28. const
  29.   DefaultServiceName = 'MyWindowsService';
  30.   DefaultServiceDisplayName = 'My Windows Service';
  31.   DefaultServiceDescription = 'This is my Windows service';
  32.  
  33. type
  34.   [RunInstaller(True)]
  35.   TServiceInstaller = class(System.Configuration.Install.Installer)
  36.   private
  37.     FServicesDependedOn: array of string;
  38.     FServiceProcessInstaller: System.ServiceProcess.ServiceProcessInstaller;
  39.     FServiceInstaller: System.ServiceProcess.ServiceInstaller;
  40.  
  41.     procedure AddServiceDescription(ServiceDesc: string);
  42.   strict protected
  43.     procedure OnBeforeInstall(savedState: IDictionary); override;
  44.     procedure OnAfterInstall(savedState: IDictionary); override;
  45.     procedure OnBeforeUninstall(savedState: IDictionary); override;
  46.     procedure OnAfterUninstall(savedState: IDictionary); override;
  47.   public
  48.     constructor Create;
  49.   end;
  50.  
  51. var
  52.   // This is needed for Borland Delphi for .NET to emit TServiceInstaller class info
  53.   ServiceInstaller: TServiceInstaller = nil;
  54.  
  55. implementation
  56.  
  57. { TServiceInstaller }
  58.  
  59. constructor TServiceInstaller.Create;
  60. begin
  61.   inherited Create;
  62.  
  63.   //ToDo: add more service dependencies here
  64.   SetLength(FServicesDependedOn, 1);
  65.   FServicesDependedOn[0] := 'Event Log';
  66.  
  67.   FServiceProcessInstaller :=
  68.     System.ServiceProcess.ServiceProcessInstaller.Create;
  69.   FServiceProcessInstaller.Account :=
  70.     System.ServiceProcess.ServiceAccount.LocalSystem;
  71.   FServiceProcessInstaller.Password := '';
  72.   FServiceProcessInstaller.Username := '';
  73.  
  74.   FServiceInstaller := System.ServiceProcess.ServiceInstaller.Create;
  75.   FServiceInstaller.ServiceName := DefaultServiceName;
  76.   FServiceInstaller.DisplayName := DefaultServiceDisplayName;
  77.   //ToDo: adjust service type to Manual if necessary
  78.   FServiceInstaller.StartType :=
  79.     System.ServiceProcess.ServiceStartMode.Automatic;
  80.   FServiceInstaller.ServicesDependedOn := FServicesDependedOn;
  81.  
  82.   Installers.Add(FServiceProcessInstaller);
  83.   Installers.Add(FServiceInstaller);
  84. end;
  85.  
  86. // Note that Description will be automatically deleted along with the
  87. // service registry key when the service's uninstalled
  88. procedure TServiceInstaller.AddServiceDescription(ServiceDesc: string);
  89. var
  90.   ServiceKey: RegistryKey;
  91. begin
  92.   try
  93.     ServiceKey := Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
  94.       'SYSTEM\CurrentControlSet\Services\' + DefaultServiceName, True);
  95.   except
  96.     ServiceKey := nil;
  97.   end;
  98.  
  99.   if (ServiceKey <> nil) then
  100.   begin
  101.     try
  102.       ServiceKey.SetValue('Description', ServiceDesc);
  103.     except
  104.     end;
  105.  
  106.     ServiceKey.Close;
  107.   end;
  108. end;
  109.  
  110. procedure TServiceInstaller.OnBeforeInstall(savedState: IDictionary);
  111. begin
  112.   //ToDo: add your code here
  113.  
  114.   inherited;
  115. end;
  116.  
  117. procedure TServiceInstaller.OnAfterInstall(savedState: IDictionary);
  118. begin
  119.   inherited;
  120.  
  121.   AddServiceDescription(DefaultServiceDescription);
  122.   
  123.   //ToDo: add your code here
  124. end;
  125.  
  126. procedure TServiceInstaller.OnBeforeUninstall(savedState: IDictionary);
  127. begin
  128.   //ToDo: add your code here
  129.  
  130.   inherited;
  131. end;
  132.  
  133. procedure TServiceInstaller.OnAfterUninstall(savedState: IDictionary);
  134. begin
  135.   inherited;
  136.  
  137.   //ToDo: add your code here
  138. end;
  139.  
  140. end.
  141.